package com.luoyi.service.ehr;

import cn.hutool.core.util.EnumUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.luoyi.service.ehr.enumeration.DegreeType;
import com.luoyi.service.ehr.enumeration.EducationalBackgroundType;
import com.luoyi.service.ehr.enumeration.YesOrNoEnum;
import lombok.extern.slf4j.Slf4j;

/**
 * 教育经历处理类
 *
 * @author xt
 */
@Slf4j
public class EducationalExperienceProcess {

    /**
     * 教育经历调试json
     */
    private static final String TEXT = "[{\"F1280\":\"{\\\"KEY\\\":\\\"2\\\",\\\"VALUE\\\":\\\"材料类\\\"}\",\"F1290\":\"{\\\"KEY\\\":\\\"00000001\\\",\\\"VALUE\\\":\\\"统招\\\"}\",\"F1260\":\"20110901\",\"F1270\":\"湖南大学\",\"F1295\":\"{\\\"KEY\\\":\\\"01\\\",\\\"VALUE\\\":\\\"全日制\\\"}\",\"F1275\":\"{\\\"KEY\\\":\\\"30\\\",\\\"VALUE\\\":\\\"本科\\\"}\",\"F1285\":\"材料\",\"F1296\":\"{\\\"KEY\\\":\\\"3\\\",\\\"VALUE\\\":\\\"学士学位\\\"}\",\"F1277\":\"\",\"F1265\":\"20150630\",\"F1276\":\"{\\\"KEY\\\":\\\"X\\\",\\\"VALUE\\\":\\\"是\\\"}\",\"F1278\":\"{\\\"KEY\\\":\\\"\\\",\\\"VALUE\\\":\\\"否\\\"}\",\"F1300\":\"{\\\"KEY\\\":\\\"\\\",\\\"VALUE\\\":\\\"\\\"}\",\"F1305\":\"111\"},{\"F1280\":\"{\\\"KEY\\\":\\\"1\\\",\\\"VALUE\\\":\\\"机械类\\\"}\",\"F1290\":\"{\\\"KEY\\\":\\\"00000012\\\",\\\"VALUE\\\":\\\"中等教育\\\"}\",\"F1260\":\"20090901\",\"F1270\":\"安化县二中\",\"F1295\":\"{\\\"KEY\\\":\\\"01\\\",\\\"VALUE\\\":\\\"全日制\\\"}\",\"F1275\":\"{\\\"KEY\\\":\\\"90\\\",\\\"VALUE\\\":\\\"高中\\\"}\",\"F1285\":\"\",\"F1296\":\"{\\\"KEY\\\":\\\"0\\\",\\\"VALUE\\\":\\\"无学位\\\"}\",\"F1277\":\"\",\"F1265\":\"20110630\",\"F1276\":\"{\\\"KEY\\\":\\\"\\\",\\\"VALUE\\\":\\\"否\\\"}\",\"F1278\":\"{\\\"KEY\\\":\\\"X\\\",\\\"VALUE\\\":\\\"是\\\"}\",\"F1300\":\"{\\\"KEY\\\":\\\"91\\\",\\\"VALUE\\\":\\\"普通高中毕业\\\"}\",\"F1305\":\"111111\"}]";

    public static void main(String[] args) {
        String execute = execute(TEXT);
        System.out.println(execute);
    }


    /**
     * 开始时间
     */
    private static final String START_TIME = "F1260";
    /**
     * 结束时间
     */
    private static final String END_TIME = "F1265";


    /**
     * 专业类别
     */
    private static final String PROFESSIONAL_CATEGORY = "F1280";
    /**
     * 教育类型
     */
    private static final String TYPE_OF_EDUCATION = "F1290";
    /**
     * 学校名称（名称务必与毕业证上保持一致）
     */
    private static final String SCHOOL = "F1270";
    /**
     * 学习形式
     */
    private static final String LEARNING_FORM = "F1295";
    /**
     * 学历
     */
    private static final String EDUCATIONAL_BACKGROUND = "F1275";
    /**
     * 专业名称（初中以上必填）
     */
    private static final String PROFESSIONAL_NAME = "F1285";
    /**
     * 学位
     */
    private static final String DEGREE_TITLE = "F1296";

    /**
     * 是否最高学位
     */
    private static final String HIGHEST_DEGREE_OR_NOT = "F1277";
    /**
     * 是否最高学历
     */
    private static final String HIGHEST_EDUCATIONAL_BACKGROUND_OR_NOT = "F1276";
    /**
     * 是否初始学历
     */
    private static final String INITIAL_EDUCATION = "F1278";


    /**
     * 证书
     */
    private static final String CERTIFICATE = "F1300";
    /**
     * 证书编号
     */
    private static final String ACADEMIC_CERTIFICATE_NUMBER = "F1305";


    /**
     * 执行
     */
    public static String execute(String sourceJonStr) {
        JSONArray iList = JSON.parseArray(sourceJonStr);
        // 最高学历索引
        int highestQualificationsIndex = 0;
        Integer highestQualifications = null;
        // 初始学历索引
        int initialEducationIndex = 0;
        Integer initialEducation = null;
        // 最高学位索引
        int highestDegreeIndex = 0;
        Integer highestDegree = null;
        for (int i = 0; i < iList.size(); i++) {
            JSONObject jsonItem = iList.getJSONObject(i);
            // 学历
            String educationalBackground = jsonItem.getString(EDUCATIONAL_BACKGROUND);
            if (StrUtil.isNotEmpty(educationalBackground) && JSONUtil.isTypeJSON(educationalBackground)) {
                JSONObject jsonObject = JSON.parseObject(educationalBackground);
                Integer key = jsonObject.getInteger("KEY");
                if (key != null) {
                    // 最高学历
                    if (highestQualifications == null) {
                        highestQualifications = key;
                        highestQualificationsIndex = i;
                    } else if (highestQualifications > key) {
                        highestQualifications = key;
                        highestQualificationsIndex = i;
                    }

                    // 初始学历
                    if (initialEducation == null) {
                        initialEducation = key;
                        initialEducationIndex = i;
                    } else if (initialEducation < key) {
                        initialEducation = key;
                        initialEducationIndex = i;
                    }
                }
            }

            // 最高学位索引
            String degreeTitle = jsonItem.getString(DEGREE_TITLE);
            if (StrUtil.isNotEmpty(degreeTitle) && JSONUtil.isTypeJSON(degreeTitle)) {
                JSONObject jsonObject = JSONObject.parseObject(degreeTitle);
                Integer key = jsonObject.getInteger("KEY");
                if (key != null && !DegreeType.KEY0.getKey().equals(key.toString())) {
                    if (highestDegree == null) {
                        highestDegree = key;
                        highestDegreeIndex = i;
                    } else if (highestDegree > key) {
                        highestDegree = key;
                        highestDegreeIndex = i;
                    }
                }
            }
        }

        for (int i = 0; i < iList.size(); i++) {
            JSONObject jsonItem = iList.getJSONObject(i);
            // 是否最高学位
            if (highestDegree == null) {
                jsonItem.put(HIGHEST_DEGREE_OR_NOT, StrUtil.EMPTY);
            } else {
                if (highestDegreeIndex == i) {
                    jsonItem.put(HIGHEST_DEGREE_OR_NOT, YesOrNoEnum.X.getJsonStr());
                } else {
                    jsonItem.put(HIGHEST_DEGREE_OR_NOT, YesOrNoEnum.N.getJsonStr());
                }
            }

            // 最高学历
            if (highestQualifications == null) {
                jsonItem.put(HIGHEST_EDUCATIONAL_BACKGROUND_OR_NOT, StrUtil.EMPTY);
            } else {
                if (highestQualificationsIndex == i) {
                    jsonItem.put(HIGHEST_EDUCATIONAL_BACKGROUND_OR_NOT, YesOrNoEnum.X.getJsonStr());
                } else {
                    jsonItem.put(HIGHEST_EDUCATIONAL_BACKGROUND_OR_NOT, YesOrNoEnum.N.getJsonStr());
                }
            }

            // 初始学历
            if (initialEducation == null) {
                jsonItem.put(INITIAL_EDUCATION, StrUtil.EMPTY);
            } else {
                if (initialEducationIndex == i) {
                    jsonItem.put(INITIAL_EDUCATION, YesOrNoEnum.X.getJsonStr());
                } else {
                    jsonItem.put(INITIAL_EDUCATION, YesOrNoEnum.N.getJsonStr());
                }
            }
        }
        log.info("最高学历是：{}", highestQualifications == null ? "无" : EnumUtil.getBy(EducationalBackgroundType::getKey, highestQualifications + "").getValue());
        log.info("初始学历是：{}", initialEducation == null ? "无" : EnumUtil.getBy(EducationalBackgroundType::getKey, initialEducation + "").getValue());
        log.info("最高学位：{}", highestDegree == null ? "无" : EnumUtil.getBy(DegreeType::getKey, highestDegree + "").getValue());
        return JSON.toJSONString(iList);
    }


}
